package tyk.utils;

import tyk.Service.EmailService;
import tyk.pojo.Email;

import java.sql.SQLException;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;


/***
 *@title SendEmail
 *@description <TODO description class purpose>
 *@author Tang'ya'kang
 *@VERSION 1.0.0
 *@CREATE 2023/5/28 11:44
 **/
public class SendEmail {

        String username = "3154522735@qq.com"; // 接收方邮件地址
        final String password = "fldbdlyvndyydfbc"; // 接收方邮件密码或授权码
        String fromEmail = "3154522735@qq.com"; // 发送方邮件地址
        private String toEmail = "2201976373@qq.com"; // 接收方邮件地址
        private String user_name;

    public void setUser_name(String user_name) {
        this.user_name = user_name;
    }

    public String getToEmail() {
        return toEmail;
    }

    public void setToEmail(String toEmail) {
        this.toEmail = toEmail;
    }
    public void run1(){
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.qq.com");
        props.put("mail.smtp.port", "465"); // or 587
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.ssl.enable", "true"); // if SSL is enabled
        props.put("mail.smtp.starttls.enable", "true"); // if TLS is enabled

        Session session = Session.getInstance(props,
                new Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(username, password);
                    }
                });

        try {
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(fromEmail));
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse(toEmail));
            message.setSubject("Only__Book");
            Integer token=RandomNumber.run();
            String value="http://localhost:8080/JaveWeb/emailServlet?action=EmailActivation&user_name="+user_name+"&email="+toEmail+"&token="+token;
            //jdbc操作
            EmailService emailService=new EmailService();
            Email email=new Email(user_name, new Timestamp(System.currentTimeMillis() + 10 * 60 * 1000),token);
            emailService.insert(email);
            String Text="【Only__Book】您好，感谢您注册我们的服务。您的邮箱激活链接如下所示：\n" +
                    value +
                    "\n请在10分钟内点击该链接，以绑定您的邮箱账户。\n" +
                    "如果您遇到任何问题，请随时与我们联系。";
            message.setText(Text);

            Transport.send(message);

            System.out.println("Email sent successfully");

        } catch (MessagingException | SQLException e) {
            throw new RuntimeException(e);
        }
    }
    public void run2(){
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.qq.com");
        props.put("mail.smtp.port", "465"); // or 587
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.ssl.enable", "true"); // if SSL is enabled
        props.put("mail.smtp.starttls.enable", "true"); // if TLS is enabled

        Session session = Session.getInstance(props,
                new Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(username, password);
                    }
                });

        try {
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(fromEmail));
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse(toEmail));
            message.setSubject("Only__Book");
            Integer token=RandomNumber.run();
            String value="http://localhost:8080/JaveWeb/emailServlet?action=EditPassword&user_name="+user_name+"&email="+toEmail+"&token="+token;
            //jdbc操作
            EmailService emailService=new EmailService();
            Email email=new Email(user_name, new Timestamp(System.currentTimeMillis() + 10 * 60 * 1000),token);
            emailService.insert(email);
            String Text="尊敬的用户，您正在进行密码重置操作，请确认以下信息：\n" +
                    "用户名："+user_name+"\n" +
                    "绑定的邮箱："+toEmail+"\n" +
                    "如果上述信息正确，请在10分钟内点击以下链接重置密码：\n" +
                    value+"\n" +
                    "\n" +
                    "如果您没有进行密码重置操作，请忽略此邮件。\n" +
                    "\n" +
                    "提示：请勿将此邮件转发或透露给他人。";
            message.setText(Text);

            Transport.send(message);

            System.out.println("Email sent successfully");

        } catch (MessagingException | SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
